//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.customer;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperManager;
import net.sf.jasperreports.view.JRViewer;
import org.jpgolay.me.util.MEDate;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.customer:
//            CustomerPrintDataSource, Customer
public class CustomerPrint {

    public CustomerPrint(Customer customer, Object familyDiscount[][], Object itemDiscount[][], boolean viewer, Parameter param) {
        print(customer, familyDiscount, itemDiscount, viewer, param);
    }

    private void print(Customer customer, Object familyDiscount[][], Object itemDiscount[][], boolean viewer, Parameter param) {
        try {
            Map parameters = new HashMap();
            parameters.put("ReportTitle", "Customer");
            parameters.put("DataFile", "SalesPersonPrintDataSource.java");
            parameters.put("UserId", param.getUserId());
            parameters.put("DateTime", (new MEDate()).getDateTime());
            net.sf.jasperreports.engine.JasperPrint jprint = JasperManager.fillReport("rep/customer.jasper", parameters, new CustomerPrintDataSource(customer, familyDiscount, itemDiscount, param));
            if (viewer) {
                JRViewer aViewer = new JRViewer(jprint);
                JInternalFrame aFrame = new JInternalFrame("Client");
                aFrame.getContentPane().add(aViewer);
                aFrame.setFrameIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/arrowRight18x18.png")));
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                aFrame.setSize(screenSize.width / 2, screenSize.height / 2);
                Insets insets = aFrame.getInsets();
                aFrame.setSize(aFrame.getWidth() + insets.left + insets.right, aFrame.getHeight() + insets.top + insets.bottom + 20);
                aFrame.setLocation((screenSize.width - aFrame.getWidth()) / 2, (screenSize.height - aFrame.getHeight()) / 2);
                aFrame.setIconifiable(false);
                aFrame.setResizable(true);
                aFrame.setMaximizable(true);
                aFrame.setClosable(true);
                param.getDesktop().add(aFrame, JLayeredPane.DEFAULT_LAYER);
                aFrame.setVisible(true);
            } else {
                JasperManager.printReport(jprint, false);
            }
        } catch (JRException e) {
            e.printStackTrace();
        }
    }
}
